package com.hnust.wswx.moblieStuBack.service.impl;

import com.hnust.wswx.moblieStuBack.service.imgUploadService;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @Data: 2022/4/25 23:48
 */

@Service("imgUploadService")
public class imgUploadServiceImpl implements imgUploadService {

    @Override
    public String upLoad(byte[] byteFile, String ext_file, String file_Name) {
        try {
            //加载fastDFS客户端的配置文件
            ClientGlobal.initByProperties("config/fastDfs-client.properties");
            //创建tracker的客户端
            TrackerClient tracker = new TrackerClient();
            TrackerServer trackerServer = tracker.getConnection();
            //定义storage的客户端
            StorageClient1 client = new StorageClient1(trackerServer, null);
            //文件元信息
            NameValuePair[] metaList = new NameValuePair[1];
            metaList[0] = new NameValuePair("fileName", file_Name + "." + ext_file);
            //通过文件字节流执行上传
            String fileId = client.upload_file1(byteFile, ext_file, metaList);
            //System.out.println("upload success. file id is: " + fileId);
            //关闭trackerServer的连接
            trackerServer.close();
            return fileId;
        } catch (IOException | MyException e) {
            e.printStackTrace();
            return null;
        }
    }
}
